﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Library
{
    public class adminNhanvien
    {
        private SqlConnection sqlcon;
        private SqlCommand sqlcmd;
        public static SqlConnection GetConnection()
        {
            string strConnect = ConfigurationManager.ConnectionStrings["StrConnectString"].ToString();
            SqlConnection sqlcon = new SqlConnection(strConnect);
            return sqlcon;
        }

        public DataTable getdata(Int16 type)
        {
            DataTable dt = new DataTable();
            try
            {
                using (sqlcon = GetConnection())
                {
                    sqlcon.Open();
                    sqlcmd = new SqlCommand("st_admin_nhanvien_danhsach", sqlcon);
                    sqlcmd.CommandType = CommandType.StoredProcedure;
                    sqlcmd.CommandTimeout = 180;
                    sqlcmd.Parameters.Add("@type", SqlDbType.SmallInt).Value = type;
                    SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
                    da.Fill(dt);
                    sqlcon.Close();
                }
            }
            catch { }
            finally { sqlcmd.Dispose(); sqlcon.Dispose(); }
            return dt;
        }

        public int them(string ten,string vitri,string username,string password,Int16 trangthai)
        {
            int rtn = -1;
            try
            {
                using (sqlcon = GetConnection())
                {
                    sqlcon.Open();
                    sqlcmd = new SqlCommand("st_admin_nhanvien_themmoi", sqlcon);
                    sqlcmd.CommandType = CommandType.StoredProcedure;
                    sqlcmd.CommandTimeout = 180;
                    sqlcmd.Parameters.Add("@ten", SqlDbType.NVarChar, 50).Value = ten;
                    sqlcmd.Parameters.Add("@vitri", SqlDbType.NVarChar, 50).Value = vitri;
                    sqlcmd.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value = username;
                    sqlcmd.Parameters.Add("@password", SqlDbType.NVarChar, 50).Value = password;
                    sqlcmd.Parameters.Add("@trangthai", SqlDbType.SmallInt).Value = trangthai;
                    sqlcmd.ExecuteNonQuery();
                    sqlcon.Close();
                    rtn = 1;
                }
            }
            catch { }
            finally { sqlcmd.Dispose(); sqlcon.Dispose(); }
            return rtn;
        }

        public int sua(Int16 id, string ten, string vitri, string username, string password, Int16 trangthai)
        {
            int rtn = -1;
            try
            {
                using (sqlcon = GetConnection())
                {
                    sqlcon.Open();
                    sqlcmd = new SqlCommand("st_admin_nhanvien_sua", sqlcon);
                    sqlcmd.CommandType = CommandType.StoredProcedure;
                    sqlcmd.CommandTimeout = 180;
                    sqlcmd.Parameters.Add("@id", SqlDbType.SmallInt).Value = id;
                    sqlcmd.Parameters.Add("@ten", SqlDbType.NVarChar, 50).Value = ten;
                    sqlcmd.Parameters.Add("@vitri", SqlDbType.NVarChar, 50).Value = vitri;
                    sqlcmd.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value = username;
                    sqlcmd.Parameters.Add("@password", SqlDbType.NVarChar, 50).Value = password;
                    sqlcmd.Parameters.Add("@trangthai", SqlDbType.SmallInt).Value = trangthai;
                    sqlcmd.ExecuteNonQuery();
                    sqlcon.Close();
                    rtn = 1;
                }
            }
            catch { }
            finally { sqlcmd.Dispose(); sqlcon.Dispose(); }
            return rtn;
        }

        public int quyen(Int16 id, string quyen, string tenquyen)
        {
            int rtn = -1;
            try
            {
                using (sqlcon = GetConnection())
                {
                    sqlcon.Open();
                    sqlcmd = new SqlCommand("st_admin_nhanvien_quyen", sqlcon);
                    sqlcmd.CommandType = CommandType.StoredProcedure;
                    sqlcmd.CommandTimeout = 180;
                    sqlcmd.Parameters.Add("@id",SqlDbType.SmallInt).Value = id;
	                sqlcmd.Parameters.Add("@quyen",SqlDbType.NVarChar,150).Value = quyen;
                    sqlcmd.Parameters.Add("@tenquyen", SqlDbType.NVarChar, 750).Value = tenquyen;
                    sqlcmd.ExecuteNonQuery();
                    sqlcon.Close();
                    rtn = 1;
                }
            }
            catch { }
            finally { sqlcmd.Dispose(); sqlcon.Dispose(); }
            return rtn;
        }

        public DataTable chitiet(Int16 id)
        {
            DataTable dt = new DataTable();
            try
            {
                using (sqlcon = GetConnection())
                {
                    sqlcon.Open();
                    sqlcmd = new SqlCommand("st_admin_chitiet", sqlcon);
                    sqlcmd.CommandType = CommandType.StoredProcedure;
                    sqlcmd.CommandTimeout = 180;
                    sqlcmd.Parameters.Add("@id", SqlDbType.SmallInt).Value = id;
                    SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
                    da.Fill(dt);
                    sqlcon.Close();
                }
            }
            catch { }
            finally { sqlcmd.Dispose(); sqlcon.Dispose(); }
            return dt;
        }
    }
}
